<template>
  <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="register-form">
    <el-card class="section-card" shadow="never">
      <template #header>
        <div class="section-title">队长信息</div>
      </template>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用户名" prop="captain.username">
            <el-input v-model="form.captain.username" placeholder="请输入队长账号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="captain.password">
            <el-input v-model="form.captain.password" type="password" placeholder="请输入密码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名" prop="captain.name">
            <el-input v-model="form.captain.name" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学号" prop="captain.studentId">
            <el-input v-model="form.captain.studentId" placeholder="请输入学号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="手机号" prop="captain.phone">
            <el-input v-model="form.captain.phone" placeholder="请输入手机号" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="section-card" shadow="never">
      <template #header>
        <div class="section-title">队伍信息</div>
      </template>
      <el-form-item label="队伍名称" prop="teamName">
        <el-input v-model="form.teamName" placeholder="请输入队伍名称" />
      </el-form-item>
      <el-form-item label="队员信息">
        <el-table :data="form.members" border style="width: 100%">
          <el-table-column label="队员序号" type="index" width="80" />
          <el-table-column label="姓名">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="姓名" />
            </template>
          </el-table-column>
          <el-table-column label="学号">
            <template #default="scope">
              <el-input v-model="scope.row.studentId" placeholder="学号" />
            </template>
          </el-table-column>
          <el-table-column label="电话">
            <template #default="scope">
              <el-input v-model="scope.row.phone" placeholder="电话" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button type="danger" size="small" @click="removeMember(scope.$index)" :disabled="form.members.length <= 1">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addMember" :disabled="form.members.length >= 4" style="margin-top: 10px;">添加队员</el-button>
      </el-form-item>
    </el-card>

    <el-form-item>
      <el-button type="primary" @click="submitForm" :loading="loading">提交注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const formRef = ref(null)
const loading = ref(false)
const form = ref({
  captain: {
    username: '',
    password: '',
    name: '',
    studentId: '',
    phone: ''
  },
  teamName: '',
  members: [
    { name: '', studentId: '', phone: '' }
  ]
})

const rules = {
  'captain.username': [{ required: true, message: '请输入队长账号', trigger: 'blur' }],
  'captain.password': [{ required: true, message: '请输入密码', trigger: 'blur' }],
  'captain.name': [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  'captain.studentId': [{ required: true, message: '请输入学号', trigger: 'blur' }],
  'captain.phone': [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  teamName: [{ required: true, message: '请输入队伍名称', trigger: 'blur' }],
  // 队员校验在提交时额外处理
}

const addMember = () => {
  if (form.value.members.length < 4) {
    form.value.members.push({ name: '', studentId: '', phone: '' })
  }
}
const removeMember = (idx) => {
  if (form.value.members.length > 1) {
    form.value.members.splice(idx, 1)
  }
}

const validateMembers = () => {
  if (form.value.members.length < 1) {
    ElMessage.error('至少需要1名队员')
    return false
  }
  for (let i = 0; i < form.value.members.length; i++) {
    const m = form.value.members[i]
    if (!m.name || !m.studentId || !m.phone) {
      ElMessage.error(`请完整填写队员${i + 1}信息`)
      return false
    }
  }
  return true
}

const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return
    if (!validateMembers()) return
    loading.value = true
    try {
      const res = await axios.post('/api/students/register', form.value)
      if (res.data.code === 200) {
        ElMessage.success('注册成功！')
        // 可跳转或重置表单
        form.value = {
          captain: { username: '', password: '', name: '', studentId: '', phone: '' },
          teamName: '',
          members: [{ name: '', studentId: '', phone: '' }]
        }
      } else {
        ElMessage.error(res.data.message || '注册失败')
      }
    } catch (e) {
      ElMessage.error(e?.response?.data?.message || '注册失败')
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.register-form {
  max-width: 800px;
  margin: 30px auto;
  background: #fff;
  padding: 24px 32px 32px 32px;
  border-radius: 10px;
  box-shadow: 0 2px 16px 0 rgba(60,90,150,0.08);
}
.section-card {
  margin-bottom: 24px;
  border: 1px solid #bfc8da;
  border-radius: 8px;
  padding: 18px 18px 0 18px;
}
.section-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
  border: 2px solid #333;
  display: inline-block;
  padding: 2px 16px;
  border-radius: 4px;
  background: #f8faff;
}
</style>
